//
//  C_2ViewController.m
//  RACExamale
//
//  Created by cym_bj on 2017/11/30.
//  Copyright © 2017年 com.cym. All rights reserved.
//

#import "C_2ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "NSObject+RACKVOWrapper.h"
@interface C_2ViewController ()

@property (nonatomic,strong)UIView *bgView;

@end

@implementation C_2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"代替KVO";
    
    self.bgView=[[UIView alloc]init];
    self.bgView.frame=CGRectMake(100, 100, 100, 100);
    self.bgView.backgroundColor=[UIColor redColor];
    [self.view addSubview:self.bgView];
    
   //监听 frmae
    
    //方式一
    //监听的是旧值 和新值
    [self.bgView rac_observeKeyPath:@"frame" options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew) observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {

        NSLog(@"方式一: value=%@  change=%@",value ,change);

    }];
    
    //方式二
    
    [[self.bgView rac_valuesForKeyPath:@"frame" observer:self] subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"方式二:%@",x);
        
    }];
    
    
    //监听颜色
    [[self.bgView rac_valuesForKeyPath:@"backgroundColor" observer:self] subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"颜色改变:%@",x);
        
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.bgView.frame=CGRectMake(50, 150, 130, 130);
    self.bgView.backgroundColor=[UIColor blueColor];
}

@end
